﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pb;

public class RankListModel : MonoBehaviour
{

    public GameObject RankPanel;

    public GameObject Collider;
    public GameObject EarnGoldBtn;
    public GameObject AssetBtn;

    public GameObject AssetList;
    public GameObject EarnGoldList;
    private GameObject HallPanel;    

    public List<RankItem> listItem = new List<RankItem>();

    public List<GameUserInfo> UserInfoList = new List<GameUserInfo>();

    public UIGrid grid1;
    public UIGrid grid2;

    public GameObject game1;
    public GameObject game2;
   
    EginUser user = EginUser.Instance;
    /// <summary>
    /// 榜单种类  1是财富榜  2是赚金榜
    /// </summary>
    public int ListType;
    void Awake()
    {
        UIEventListener.Get(EarnGoldBtn).onClick = ButtonClick;
        UIEventListener.Get(AssetBtn).onClick = ButtonClick;
        UIEventListener.Get(Collider).onClick = ButtonClick;
    }

    void Start()
    {        
        grid1 = game1.GetComponent<UIGrid>();
        grid2 = game2.GetComponent<UIGrid>();

        HallPanel = GameObject.Find("UI Root/HallPanel");

        // LogMsg(ListType);
        //asd();
    }

    void ButtonClick(GameObject button)
    {
        HallModel hallModel = HallPanel.GetComponent<HallModel>();

        if (button.name == "Collider")
        {
            //hallModel.coinCount.transform.parent.gameObject.SetActive(true);
            //hallModel.roomCardCount.transform.parent.gameObject.SetActive(true);
            hallModel.RankListBtn.SetActive(true);
            Destroy(RankPanel);
        }
        else if (button.name == "EarnGoldBtn")
        {
            AssetList.SetActive(false);
            EarnGoldList.SetActive(true);
            //hallModel.coinCount.transform.parent.gameObject.SetActive(false);
            //hallModel.roomCardCount.transform.parent.gameObject.SetActive(false);          

            RecordListRequest lg = new RecordListRequest { Type = 2 };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(lg, MsgId.RecordListRequest);

        }
        else if (button.name == "AssetBtn")
        {
            AssetList.SetActive(true);
            EarnGoldList.SetActive(false);
            //hallModel.coinCount.transform.parent.gameObject.SetActive(true);
            //hallModel.roomCardCount.transform.parent.gameObject.SetActive(true);

            RecordListRequest lg = new RecordListRequest { Type = 1 };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(lg, MsgId.RecordListRequest);
        }


    }

    /// <summary>
    /// 设置用户排行的信息
    /// </summary>
    public void LogMsg(int type)
    {
        print("进入了" + type);
        if (UserInfoList.Count > 0)
        {
            print("执行了" + UserInfoList.Count);
            for (int i = 0; i < UserInfoList.Count; i++)
            {
                // 财富榜
                if (type == 1)
                {
                    // 实例化
                    GameObject game = Instantiate(Resources.Load(HallPrafabPath.RankItem), grid1.transform) as GameObject;
                    // 排序
                    grid1.repositionNow = true;
                    RankItem rankItem = game.GetComponent<RankItem>();
                    // 设置信息
                    rankItem.Set(UserInfoList[i].AvatarId, UserInfoList[i].Nickname, user.ReturnMoney(UserInfoList[i].BagCoinCount), type, i);
                }
                // 赚金榜
                else if (type == 2)
                {
                    // 实例化
                    GameObject game = Instantiate(Resources.Load(HallPrafabPath.RankItem), grid2.transform) as GameObject;
                    // 排序
                    grid2.repositionNow = true;
                    RankItem rankItem = game.GetComponent<RankItem>();
                    // 设置信息
                    rankItem.Set(UserInfoList[i].AvatarId, UserInfoList[i].Nickname, user.ReturnMoney(UserInfoList[i].BagCoinCount), type, i);
                }


            }
        }
    }

    public void asd()
    {
        if (game1.transform.childCount > 0)
        {
            for (int i = 0; i < game1.transform.childCount; i++)
            {
                Destroy(game1.transform.GetChild(i).gameObject);
            }
        }
        for (int i = 0; i < 5; i++)
        {

            // 实例化
            GameObject game = Instantiate(Resources.Load(HallPrafabPath.RankItem), grid1.transform) as GameObject;
            // 排序
            grid1.repositionNow = true;
            RankItem rankItem = game.GetComponent<RankItem>();
            // 设置信息
            rankItem.Set(5, "nji", "50亿", 1, i);
        }
    }
}
